home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cvs / sprite / save / find_names.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-29  |  3.9 KB  |  166 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Id: find_names.c,v 1.11 89/11/19 23:20:02 berliner Exp $";
  3. #endif !lint
  4.  
  5. /*
  6.  *    Copyright (c) 1989, Brian Berliner
  7.  *
  8.  *    You may distribute under the terms of the GNU General Public License
  9.  *    as specified in the README file that comes with the CVS 1.0 kit.
  10.  *
  11.  * Find Names
  12.  *
  13.  *    Writes all the pertinent file names, both from the administration
  14.  *    and from the repository to the argc/argv arguments.
  15.  *
  16.  *    The names should be freed by callin free_names() when they are no
  17.  *    longer needed.
  18.  *
  19.  *    Note that Find_Names() honors the administration Entries.Static file
  20.  *    to indicate that the Repository should not be searched for new files.
  21.  */
  22.  
  23. #include <sys/param.h>
  24. #include <sys/types.h>
  25. #include <dirent.h>
  26. #include "cvs.h"
  27.  
  28. Find_Names(pargc, argv, which)
  29.     int *pargc;
  30.     char *argv[];
  31.     enum ftype which;
  32. {
  33.     char dir[MAXPATHLEN], line[MAXLINELEN];
  34.     FILE *fpin;
  35.     char *cp;
  36.  
  37.     *pargc = 0;
  38.     argv[0] = NULL;            /* Assume none */
  39.     if (which == MOD) {
  40.     fpin = open_file(CVSADM_MOD, "r");
  41.     /*
  42.      * Parse the Mod file, and calling addname() for each line.
  43.      */
  44.     while (fgets(line, sizeof(line), fpin) != NULL) {
  45.         if ((cp = rindex(line, '\n')) != NULL)
  46.         *cp = '\0';
  47.         *cp = '\0';
  48.         addname(pargc, argv, line);
  49.     }
  50.     (void) fclose(fpin);
  51.     } else {
  52.     fpin = open_file(CVSADM_ENT, "r");
  53.     /*
  54.      * Only scan for ,v files if Entries.Static does not exist
  55.      */
  56.     if (!isfile(CVSADM_ENTSTAT)) {
  57.         if (find_rcs(Repository, pargc, argv) != 0)
  58.         error(1, "cannot open directory %s", Repository);
  59.         if (which == ALLPLUSATTIC) {
  60.         (void) sprintf(dir, "%s/%s", Repository, CVSATTIC);
  61.         (void) find_rcs(dir, pargc, argv);
  62.         }
  63.         if (find_links(Repository, pargc, argv) != 0)
  64.         error(1, "cannot open directory %s", Repository);
  65.     }
  66.     /*
  67.      * Parse the Entries file, and calling addname() for each one.
  68.      */
  69.     while (fgets(line, sizeof(line), fpin) != NULL) {
  70.         if ((cp = rindex(line, '|')) == NULL)
  71.         continue;
  72.         *cp = '\0';
  73.         if ((cp = rindex(line, ' ')) == NULL)
  74.         continue;
  75.         cp++;
  76.         addname(pargc, argv, cp);
  77.     }
  78.     (void) fclose(fpin);
  79.     }
  80.     /*
  81.      * And finally, sort the names so that they look reasonable
  82.      * as they are processed (there *is* order in the world)
  83.      */
  84.     qsort((char *)&argv[0], *pargc, sizeof(argv[0]), ppstrcmp);
  85. }
  86.  
  87. /*
  88.  * Finds all the ,v files in the argument directory, and adds them to the
  89.  * argv list.  Returns 0 for success and non-zero if the argument
  90.  * directory cannot be opened.
  91.  */
  92. static
  93. find_rcs(dir, pargc, argv)
  94.     char *dir;
  95.     int *pargc;
  96.     char *argv[];
  97. {
  98.     char *cp, line[50];
  99.     struct dirent *dp;
  100.     DIR *dirp;
  101.  
  102.     if ((dirp = opendir(dir)) == NULL)
  103.     return (1);
  104.     (void) sprintf(line, ".*%s$", RCSEXT);
  105.     if ((cp = re_comp(line)) != NULL)
  106.     error(0, "%s", cp);
  107.     while ((dp = readdir(dirp)) != NULL) {
  108.     if (re_exec(dp->d_name)) {
  109.         /* strip the ,v */
  110.         *rindex(dp->d_name, ',') = '\0';
  111.         addname(pargc, argv, dp->d_name);
  112.     }
  113.     }
  114.     (void) closedir(dirp);
  115.     return (0);
  116. }
  117.  
  118. /*
  119.  * Finds all the symbolic links in the argument directory, and adds them to the
  120.  * argv list.  Returns 0 for success and non-zero if the argument
  121.  * directory cannot be opened.
  122.  */
  123. static
  124. find_links(dir, pargc, argv)
  125.     char *dir;
  126.     int *pargc;
  127.     char *argv[];
  128. {
  129.     char *cp;
  130.     struct dirent *dp;
  131.     DIR *dirp;
  132.     char tmp[MAXPATHLEN];
  133.  
  134.     if ((dirp = opendir(dir)) == NULL)
  135.     return (1);
  136.     while ((dp = readdir(dirp)) != NULL) {
  137.     (void) sprintf(tmp, "%s/%s", dir, dp->d_name);
  138.     if (islink(tmp)) {
  139.         addname(pargc, argv, dp->d_name);
  140.     }
  141.     }
  142.     (void) closedir(dirp);
  143.     return (0);
  144. }
  145.  
  146. /*
  147.  * addname() adds a name to the argv array, only if it is not in
  148.  * the array already
  149.  */
  150. static
  151. addname(pargc, argv, name)
  152.     int *pargc;
  153.     char *argv[];
  154.     char *name;
  155. {
  156.     register int i;
  157.  
  158.     for (i = 0; i < *pargc; i++) {
  159.     if (strcmp(argv[i], name) == 0)
  160.         return;
  161.     }
  162.     (*pargc)++;
  163.     argv[i] = xmalloc(strlen(name) + 1);
  164.     (void) strcpy(argv[i], name);
  165. }
  166.